/**
 * swfupload gallery style settings
 *
 * @author  Yellow.Chow <aultoale@gmail.com>
 * @version $Id: settings.js 131 2013-07-24 15:56:23Z zhouyl $
 * @license http://www.gnu.org/licenses/gpl-3.0.html    GPL 3
 */
var swfu_settings = {

    /**
     * Upload backend settings
     */
    upload_url : '/swfupload.php?action=upload&directory=gallery',
    file_post_name : 'swfupload',

    /**
     * File settings
     */
    file_types : '*.jpg;*.gif;*.bmp;*.png',
    file_types_description  : '所有图片文件',
    file_size_limit : '10 MB',
    file_upload_limit : 100,
    file_queue_limit : 50,

    /**
     * Upload backend settings
     */
    flash_url : '/js/swfupload/swfupload.style.swf',
    flash9_url : '/js/swfupload/swfupload_fp9.swf',

    /**
     * Button settings
     */
    button_image_url : '/js/swfupload/active_gallery/btn_138x36.png',
    button_width : 138,
    button_height : 36,
    button_text : '',
    button_text_style : '',

    /**
     * 加载前预处理
     */
    swfupload_preload_handler : function(){
        if (!this.support.loading) {
            alert('您需要 Flash Player 9.028 以上版本');
            return false;
        }
    },

    /**
     * 加载失败
     */
    swfupload_load_failed_handler : function(){
        alert('加载上传程序时发生错误');
    },

    /**
     * 当文件成功被添加到队列中
     *
     * @param  string  file
     */
    file_queued_handler : function(file){
        var progress = new SWFU_Progress(file, this.settings.custom_settings);
        progress.init();
    },

    /**
     * 已经完成文件选择与上传队列
     *
     * @param  integer  numFilesSelected
     * @param  integer  numFilesQueued
     */
    file_dialog_complete_handler : function(numFilesSelected, numFilesQueued){
        this.startUpload();
    },

    /**
     * 文件上传队列错误
     *
     * @param  string   file
     * @param  integer  errorCode
     * @param  string   message
     */
    file_queue_error_handler : function(file, errorCode, message){
        if (errorCode == SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED) {
            alert('选择的文件数量不能超过 ' + this.settings.file_queue_limit + ' 个');
            return false;
        }

        switch (errorCode) {
            case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
                message = '文件超出限制大小';
                break;
            case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
                message = '无法上传 0 字节文件';
                break;
            case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
                message = '无效的上传文件类型';
                break;
        }

        var progress = new SWFU_Progress(file, this.settings.custom_settings);
        progress.init().error(message);
    },

    /**
     * 上传文件之前进行处理
     *
     * @param  string  file
     */
    upload_start_handler : null,

    /**
     * 上传进度处理
     *
     * @param  string   file
     * @param  integer  bytesLoaded
     * @param  integer  bytesTotal
     */
    upload_progress_handler : function(file, bytesLoaded, bytesTotal) {
        var progress = new SWFU_Progress(file, this.settings.custom_settings);
        progress.update(bytesLoaded);
    },

    /**
     * 上传错误处理
     *
     * @param  string   file
     * @param  integer  errorCode
     * @param  string  message
     */
    upload_error_handler : function(file, errorCode, message) {
        switch (errorCode) {
            case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:
                message = 'HTTP 错误';
                break;
            case SWFUpload.UPLOAD_ERROR.MISSING_UPLOAD_URL:
                message = '缺少上传网址';
                break;
            case SWFUpload.UPLOAD_ERROR.IO_ERROR:
                message = 'IO 错误';
                break;
            case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR:
                message = '安全校验错误';
                break;
            case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
                message = '上传超出限制';
                break;
            case SWFUpload.UPLOAD_ERROR.SPECIFIED_FILE_ID_NOT_FOUND:
                message = '未找到指定文件';
                break;
            case SWFUpload.UPLOAD_ERROR.FILE_VALIDATION_FAILED:
                message = '文件校验失败';
                break;
            case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
                message = '文件已取消';
                break;
            case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
                message = '上传被中断';
                break;
        }
        var progress = new SWFU_Progress(file, this.settings.custom_settings);
        progress.error(message);
    },

    /**
     * 上传成功
     *
     * @param  string  file
     * @param  string  serverData
     */
    upload_success_handler : function(file, serverData) {
        if (typeof this.settings.custom_settings.session_url == 'string') {
            $.post(this.settings.custom_settings.session_url, { session_id : this.settings.post_params.session_id });
        }
        var data = $.parseJSON(serverData);
        var progress = new SWFU_Progress(file, this.settings.custom_settings);
        if (data != null && data.success) {
            if (typeof this.settings.custom_settings.success_callback == 'function') {
                this.settings.custom_settings.success_callback(data, this.settings);
            }
            progress.complete();
        } else if (data.message) {
            progress.error(data.message);
        } else {
            progress.error('未知错误');
        }
    },

    /**
     * 完成上传
     */
    upload_complete_handler : function(){
        this.startUpload();
    }

};

/**
 * swfupload for jquery
 *
 * <example>
 * $('#element_id').swfupload({upload_url : 'swfupload.php', ...});
 * </example>
 *
 * @author  Yellow.Chow <aultoale@gmail.com>
 * @version $Id: settings.js 131 2013-07-24 15:56:23Z zhouyl $
 * @license http://www.gnu.org/licenses/gpl-3.0.html    GPL 3
 */
$.fn.swfupload = function(options){

    var placeholder_id = 'placeholder_' + Math.round(Math.random() * 1000000);
    $(this).append('<div id="' + placeholder_id + '"></div>');

    var settings = {
        button_placeholder_id : placeholder_id,
        custom_settings : { progress_target : $('<div></div>') }
    };
    $.extend(settings, swfu_settings, options);

    this.swfupload = new SWFUpload(settings);

    return this;
};